package com.famdoc.Service.Impl;

import com.famdoc.Dao.HomeFileMapper;
import com.famdoc.Pojo.dto.HomeFileForm;
import com.famdoc.Pojo.dto.Result;
import com.famdoc.Pojo.entity.Patient;
import com.famdoc.Service.HomeFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HomeFileServiceImpl implements HomeFileService {
    @Autowired
    private HomeFileMapper homeFileMapper;

    /**
     * 创建家庭档案
     * @param homeFileForm
     * @return
     */
    @Override
    public Result createHomeFile(HomeFileForm homeFileForm) {
        //查询父id是否存在
        String idNumber = homeFileForm.getIdNumber();
        Patient patient = homeFileMapper.queryPatientByIdNumber(idNumber);
        if (patient == null)
            return Result.error(201,"户主身份错误！");
        //对非户主添加homeId，先封装数据
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("parentId", patient.getId());
        paramMap.put("idNumberList", homeFileForm.getIdNumberList());

        Integer affectedRows = homeFileMapper.modifyHomeId(paramMap);
        if (affectedRows > 0 )
            return Result.success();
        else
            return Result.error(201,"创建家庭档案失败");
    }

    /**
     * 修改家庭档案
     * @param homeFileForm
     * @return
     */
    @Override
    public Result updateHomeFile(HomeFileForm homeFileForm) {
        //查询父id是否存在
        String idNumber = homeFileForm.getIdNumber();
        Patient patient = homeFileMapper.queryPatientByIdNumber(idNumber);
        if (patient == null)
            return Result.error(201,"户主身份错误！");
        Integer affectedRows = homeFileMapper.modifyHomeFile(patient.getId(), homeFileForm.getOtherIdNumber());
        if (affectedRows > 0 )
            return Result.success();
        else
            return Result.error(201,"修改家庭档案失败");
    }

    /**
     * 查询家庭档案
     * @param idNumber
     * @return
     */
    @Override
    public Result queryHomeFile(String idNumber) {
        Map<String, Object> patientMap = new HashMap<>();
        Patient patientMaster = null;
        //判断是否是户主
        Patient patient = homeFileMapper.queryPatientByIdNumber(idNumber);
        if (patient.getHomeId() != null){
            //不是户主，先查询拿到户主信息
            patientMaster = homeFileMapper.queryPatientByHomeId(patient.getHomeId());
            patientMap.put("patientMaster", patientMaster);
            //查询户主下面的家人
            List<Patient> patientList = homeFileMapper.queryPatientListById(patientMaster.getId());
            patientMap.put("patientList", patientList);
        }else {
            //TODO 可能是户主
            List<Patient> patientList = homeFileMapper.queryPatientListById(patient.getId());
            if (patientList.isEmpty()){
                return Result.success("无家庭档案");
            }
            //是户主
            patientMap.put("patientMaster", patient);
            patientMap.put("patientList", patientList);
        }
        return Result.success(patientMap);
    }
}
